<?php

namespace App\Models\Custom;

use App\Models\Balance\Balance;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

class Custom extends Model
{
    protected $table = 'custom';
    public $timestamps = false;
    protected $primaryKey = 'id';
    protected $guarded = ['_token'];

    /**
     * 列表
     */
    public static function getList(Request $request, $memberinfo, $isgetall)
    {
        $manger_id = $memberinfo['id'] ?? 0;
        $utype = $memberinfo['utype'] ?? 0;
        $cpid = $memberinfo['cpid'] ?? 0;
        $category = $request->category ?? 0;
        // $sys_dpm_id = $memberinfo['sys_dpm_id'] ?? 0;

        //关联余额表
        $query = self::with('balance')->orderBy('create_at', 'desc');

        $company_code = $request->company_code;
        if ($company_code != '') {
            $query->where('company_code', 'like', '%' . $company_code . '%');
        }

        $company_name = $request->company_name;
        if ($company_name != '') {
            $query->where('company_name', 'like', '%' . $company_name . '%');
        }

        /**
         * manger 1自己添加的数据，2所有数据
         * 销售只能看到自己添加的客户
         *
        */
        $manger = $request->manger;
        $type = $request->type;
//        if (($type == 1 && $manger == 1) || in_array($utype,['6','7'])) {
        if (($type == 1 && $manger == 1) || ($utype == 7 && $category == 1)) {
            $query->where('manger_id', '=', $manger_id);
        }

        if ($cpid > 0) {
            $query->where('cpid', $cpid);
        }

        // if ($sys_dpm_id > 0) {
        //     $query->where('sys_dpm_id', $sys_dpm_id);
        // }

        $category = $request->category;
        if ($category > 0) {
            $query->where('category', $category);
        }

        $custom_status = $request->custom_status ?? 0;
        if ($custom_status > 0) {
            $query->where('custom_status', $custom_status);
        }

        $invoice_status = $request->invoice_status ?? 0;
        if ($invoice_status > 0) {
            $query->where('invoice_status', $invoice_status);
        }

        $is_freeze = $request->is_freeze;
        if ($is_freeze > 0) {
            $query->where('is_freeze', $is_freeze);
        }
        if ($type == 1) {
            //客户信息
            $query->select(['id', 'code', 'company_code', 'company_name', 'category', 'settlement_company', 'wechat_number', 'custom_level', 'manger_id', 'custom_level_2', 'custom_level_3', 'account_name', 'bank_info', 'payment_info', 'custom_status', 'invoice_status', 'create_at','is_freeze']);
        } elseif ($type == 2) {
            //开票信息
            $query->select(['id', 'code', 'company_code', 'company_name', 'category', 'firm_name', 'credit_code',
                'firm_address', 'firm_phone_number', 'manger_id', 'firm_bank', 'firm_bank_number', 'firm_email', 'business_license',
                'account_opening_permit', 'attorney', 'custom_status', 'invoice_status', 'create_at','is_freeze']);
        } elseif ($type == 3) {
            //客户分析
            $query->select(['id', 'code', 'company_code', 'company_name', 'category', 'first_order_date', 'manger_id', 'transactions_number','previous_order_date', 'last_order_date', 'total_transaction_tonnage','settlement_company', 'custom_status', 'invoice_status','is_freeze']);
        }


        //客户分析，成交日期
        $start_time_last_order = $request->start_time_last_order;
        if ($start_time_last_order != '') {
            $query->where('last_order_date', '>=', $start_time_last_order);
        }

        $end_time_last_order = $request->end_time_last_order;
        if ($end_time_last_order != '') {
            $query->where('last_order_date', '<=', $end_time_last_order);
        }

        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            return paginateDataToArray($list);
        } else {
            $list = $query->get()->toArray();
            return ['list' => $list];
        }
    }

    public static function getone($id)
    {
        return self::with('balance')->find($id)->toArray();
    }

    /**
     * 关联余额表
     */
    public function balance()
    {
        return $this->hasOne(Balance::class, 'custom_id', 'id');
    }
}
